package chapt05.triangle;
// Triangle.java
// OpenGL SuperBible
// Demonstrates OpenGL color triangle
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;

public class Triangle
{
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// This function does any needed initialization on the rendering context.
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Black background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float windowHeight, windowWidth;
		
		// Prevent a divide by zero, when window is too short
		// (you can't make a window of zero width).
		if(h == 0)
			h = 1;
		
		// Set the viewport to be the entire window
		gl2.glViewport(0, 0, w, h);
		
		// Reset the coordinate system before modifying
		gl2.glLoadIdentity();
		
		// Keep the square square.
		
		// Window is higher than wide
		if(w <= h)
		{
			windowHeight = 250.0f * h / w;
			windowWidth = 250.0f;
		}
		else
		{
			// Window is wider than high
			windowWidth = 250.0f * w / h;
			windowHeight = 250.0f;
		}
		
		// Set the clipping volume
		gl2.glOrtho(-windowWidth, windowWidth, -windowHeight, windowHeight, 1.0f, -1.0f);
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT);
		
		// Enable smooth shading
		gl2.glShadeModel(GL2.GL_SMOOTH);
		
		// Draw the triangle
		gl2.glBegin(GL2.GL_TRIANGLES);
			// Red Apex
			gl2.glColor3ub((byte)255, (byte)0, (byte)0);
			gl2.glVertex3f(0.0f, 200.0f, 0.0f);
			
			// Green on the right bottom corner
			gl2.glColor3ub((byte)0, (byte)255, (byte)0);
			gl2.glVertex3f(200.0f, -70.0f, 0.0f);
			
			// Blue on the left bottom corner
			gl2.glColor3ub((byte)0, (byte)0, (byte)255);
			gl2.glVertex3f(-200.0f, -70.0f, 0.0f);
		gl2.glEnd();
		
		// Flush drawing commands
		gl2.glFlush();
	}
}